
package com.myway.core.spring;

import java.io.IOException;
import java.net.HttpURLConnection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor;

/**
 * @ClassName: AuthenticationSessionHttpInvokerRequestExecutor <br/>
 * @Description: TODO <br/>
 * @CreateDate: 2016年4月27日 下午4:27:41 <br/>
 * @author Toby
 * @version V1.0
 */
public class AuthenticationSessionHttpInvokerRequestExecutor extends SimpleHttpInvokerRequestExecutor{

	 //~ Static fields/initializers =====================================================================================

    @SuppressWarnings("unused")
	private static final Log logger = LogFactory.getLog(AuthenticationSessionHttpInvokerRequestExecutor.class);

    //~ Methods ========================================================================================================

    /**
     * Provided so subclasses can perform additional configuration if required (eg set additional request
     * headers for non-security related information etc).
     *
     * @param con the HTTP connection to prepare
     * @param contentLength the length of the content to send
     *
     * @throws IOException if thrown by HttpURLConnection methods
     */
    protected void doPrepareConnection(HttpURLConnection con, int contentLength)
        throws IOException {}

    /**
     * Called every time a HTTP invocation is made.<p>Simply allows the parent to setup the connection, and
     * then adds an <code>Authorization</code> HTTP header property that will be used for BASIC authentication.</p>
     *  <p>The <code>SecurityContextHolder</code> is used to obtain the relevant principal and credentials.</p>
     *
     * @param con the HTTP connection to prepare
     * @param contentLength the length of the content to send
     *
     * @throws IOException if thrown by HttpURLConnection methods
     * @throws AuthenticationCredentialsNotFoundException if the <code>SecurityContextHolder</code> does not contain a
     *         valid <code>Authentication</code> with both its <code>principal</code> and <code>credentials</code> not
     *         <code>null</code>
     */
    protected void prepareConnection(HttpURLConnection con, int contentLength)
        throws IOException {
        super.prepareConnection(con, contentLength);
//        con.setRequestProperty("Airline", ConfigurationUtils.getAirline());
//        HttpServletRequest request=UserUtils.getServletRequest();
//        con.setRequestProperty("RemoteIp", NetworkUtil.getIpAddress(request));
        
//        Authentication auth = (Authentication)request.getSession().getAttribute(UserUtils.AUTHENTICATION_KEY);

//        if ((auth != null) && (auth.getName() != null) && (auth.getCredentials() != null)) {
//            String base64 = auth.getName() + ":" + auth.getCredentials().toString();
//            con.setRequestProperty("Authorization", "Basic " + new String(Base64.encodeBase64(base64.getBytes())));
//          
//            if (logger.isDebugEnabled()) {
//                logger.debug("HttpInvocation now presenting via BASIC authentication SecurityContextHolder-derived: "
//                    + auth.toString());
//            }
//        } else {
//            if (logger.isDebugEnabled()) {
//                logger.debug("Unable to set BASIC authentication header as SecurityContext did not provide "
//                        + "valid Authentication: " + auth);
//            }
//        }

        doPrepareConnection(con, contentLength);
    }

}
